package com.qhit.service.impl;

import com.qhit.mapper.GoodsMapper;
import com.qhit.pojo.Goods;
import com.qhit.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;


    public List<Goods> getList(HashMap hashMap) {
        return goodsMapper.getList(hashMap);
    }

    public int getListCount(HashMap hashMap) {
        return goodsMapper.getListCount(hashMap);
    }

    public int add(Goods goods) {
        return goodsMapper.add(goods);
    }

    public Goods getGoodsById(int id) {
        return goodsMapper.getGoodsById(id);
    }

    public int edit(Goods g) {
        return goodsMapper.edit(g);
    }

    public int del(int id) {
        return goodsMapper.del(id);
    }

    public Map getListByPageWithKeywords(Integer page, Integer pageSize, String keywords) {
        HashMap hashMap = new HashMap();
        hashMap.put("keywords",keywords);
        hashMap.put("start",(page-1)*pageSize);
        hashMap.put("pageSize",pageSize);
        hashMap.put("page",page);
        List<Goods> goodsList=goodsMapper.getList(hashMap);
        int total= goodsMapper.getListCount(hashMap);
        hashMap.put("totalPage",(total%pageSize==0)?(total/pageSize):(total/pageSize+1));
        HashMap result = new HashMap();
        result.put("goodsList",goodsList);
        result.put("myParams",hashMap);
        return result;
    }
}
